<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>自定义绘制面形状</title>
    <link href='bundle.css' rel='stylesheet'/>
    <!-- 引入js文件 -->
    <script src="../static/js/bundle.js"></script>
    <script src="../static/js/config.js"></script>
</head>
<body>
<div id="map" style='width: 1440px; height: 800px;'></div>
<div>
    <button onclick="startDrawLine()">开始画面</button>
    <button onclick="finishDrawLine()">结束画面</button>
</div>
<script type="application/javascript">
    var vectorMap = new creeper.VectorMap('map', config.wmapId, config.mapBaseUrl);

    var geoJson = {
        "type": "FeatureCollection",
        "features": [{
            "type": "Feature",
            "geometry": {
                "coordinates": [[]],
                "type": "Polygon"
            }
        }]
    };

    var listener = function (e) {
        var lngLat = e.lngLat;

        geoJson.features[0].geometry.coordinates[0].push(lngLat.toArray());

        vectorMap.getSource('polygonSource').setData(geoJson);
    };

    vectorMap.on('load', function () {
        vectorMap.addSource('polygonSource', {
            type: 'geojson',
            data: geoJson
        });
        vectorMap.addLayer({
            type: 'fill',
            source: 'polygonSource',
            //id唯一
            id: 'polygonLayer',
            paint: {
                'fill-color': 'red'
            }
        });
    });

    function startDrawLine() {
        geoJson.features[0].geometry.coordinates = [[]];

        vectorMap.on('click',listener)
    }

    function finishDrawLine() {
        vectorMap.off('click',listener)
    }
</script>
</body>
</html>